#!/usr/bin/python
# -*- coding: utf-8 -*-
# vim: set fileencoding=utf-8

import sys, os, getopt, re
import codecs
#from pysqlite2 import dbapi2 as sqlite
import sqlite3


def public_tail(fout, have_nav=0, htmlprefix='', count=0, page=0):
    tail = u"""
           </div>
           </div>
        </div>
    """
    if have_nav == 1:
        prev = page - 1
        next = page + 1
        tail += u"""<div class="pagenav">"""
        if page > 0:
            tail += u"""<a href="%s%d.html">&lt;&lt;前一頁</a>&nbsp;&nbsp;&nbsp;&nbsp;""" % (htmlprefix, prev)
        tail += u"""第"""
        for i in range(0, count):
            if page != i:
                tail += u"""&nbsp;<a href="%s%d.html">%d</a>""" % (htmlprefix, i, i+1)
            else:
                tail += u"""&nbsp;%d""" % (i+1)
        tail += u"""&nbsp;頁"""
        if page+1 < count:
            tail += u"""&nbsp;&nbsp;&nbsp;&nbsp;<a href="%s%d.html">下一頁&gt;&gt;</a>""" % (htmlprefix, next)
        tail += u"""</div>"""
    elif have_nav == 2:
        tail += u"""<div class="foot">&nbsp;<a href="JavaScript:window.history.go(-1)" title="回到上一頁面">&lt;&lt;返回</a></div>
"""

    tail += u"""
      </div>
    </div>
    <div id="pagebottom">&nbsp;</div>
  </div>

  <div id="copy_info">推薦分辨率：1024×768，全屏瀏覽<br/>&copy;<a href="http://yinglong.org/"> Draconian Zone</a> 2008, all rights reserved
  </div>
</div>
</body>
</html>
    """
    fout.write(tail)

def feedback_window(fout):
    str = u"""
<div id="window">
   <div class="note_title" onmousedown="beginDrag(this.parentNode, event);">
      <span style="padding: 2px; float: right;">
         <button class="close" onclick="hideWindow();">×</button>
      </span>
      <span style="padding: 4px; color: #FFF; font-weight: bold;">編輯留言</span>
   </div>
<div><iframe id="frame" class="note" src="http://yinglong.org/forum/post.php?fid=6"></iframe></div>
</div>
    """
    fout.write(str)

def public_body(fout, row, imgpath):
    if row['accuracy'] != 0:
        data = re.sub(r'<img(\s*\S*?\s*)src="([\w-]*\.\w*)"(\s*\S*?\s*)[/]??>', r'<img alt="\2" \1src="' + imgpath + r'\2"\3/>', row['data'])
    else:
        data = u"""<p>本词条还没有翻译。您可以使用以下工具搜索英文版资料：</p>
<p>
<a target="_blank" href="http://www.sitelevel.com/query_v2.go?crid=496f32e1147dd5a2&amp;query=%s&amp;slice_title=&amp;page=1">在theserenedragon.net上搜索</a>  |
<a target="_blank" href="http://www.google.com/search?hl=en&amp;q=%s">使用google搜索</a>  |
<a target="_blank" href="http://dracopedia.wikispaces.com/search/view/%s">在dracopedia上搜索</a>  |
<a target="_blank" href="http://www.google.com/cse?cx=008725003508402829375%%3Afsydnxovpzu&amp;ie=UTF-8&amp;q=%s&amp;sa=Search">在The Circle of the Dragon上搜索</a>
</p>
        """ % (row['title'], row['title'], row['title'], row['title'])
    #data2 = re.sub(r'<p>', r'<p class="Normal">', data)
    accuracy_dict = {0:'transnone', 1:'translow', 2:'transhigh'}
    accuracy = accuracy_dict[row['accuracy']]
    div = u"""
<div class="h3" id="%s" title="%s">
<h3 class="%s">%s</h3>
<span class="h4"><span class="conr"><button class="opencomment" onclick="OpenComment('%s');">纠错/反馈</button></span><span>所属文化：%s</span></span>
    """ % (row['id'], row['title'], accuracy, row['title'], u'【纠错/反馈】'+row['title'], row['culture'])
    #if row['resume'] != '':
    #    div += u"""<h5>摘要</h5>\n<ul>"""
    #    resumes = row['resume'].strip().split('\n')
    #    for s in resumes:
    #        div += u"""<li>%s</li>""" % s
    #    div += u"""</ul>"""
    div += u"""
%s
</div>
    """ % data
    fout.write(div)


